import java.io.InputStream;

/**
 * @Description （class和ClassLoader）getResourceAsStream加载资源文件测试
 * @Author shixiaofei
 * @Date 2019/7/27 21:56
 * @Version 1.0
 */
public class GetResourceAsStream {

    public static void main(String[] args) {
        System.out.println("1.test文件夹已设置为Resource Folder,资源文件在test目录中");
        demo1();
        System.out.println("2.资源文件在src目录下");
        demo2();
        System.out.println("3.资源文件和类在同一目录");
        demo3();
        System.out.println("4.资源在src根目录下");
        demo4();

        //使用绝对路径，否则无法读取config.properties
        //InputStream inStream=new FileInputStream("F:\\android\\test\\src\\com\\ljq\\test\\resource\\xxx.xml");
        //ReflectTest.class.getClassLoader().getResourceAsStream(String path): 默认则是从ClassPath根下获取，path不能以’/'开头，最终是由ClassLoader获取资源。
        //InputStream inStream = ReflectTest.class.getClassLoader().getResourceAsStream("com/ljq/test/resource/xxx.xml");
        // ReflectTest.class.getResourceAsStream(String path)： path不以’/'开头时默认是从此类所在的包下取资源，以’/'开头则是从ClassPath根下获取。
        //其只是通过path构造一个绝对路径，最终还是由ClassLoader获取资源。
        //InputStream inStream = ReflectTest.class.getResourceAsStream("/com/ljq/test/resource/xxx.xml");
        //config.properties配置文件所在目录是ReflectTest类所在子目录，才可以；否则报空指针异常
//        InputStream inStream = GetResourceAsStream.class
//                .getResourceAsStream("/resource/xxxxml");
//
//        Properties props=new Properties();
//        props.load(inStream);
//        inStream.close();
//
//        String className=props.getProperty("className");
//        Collection collection=(Collection)Class.forName(className).newInstance();
//        collection.add("123");
//        System.out.println("size="+collection.size()); //size=1

//	不以’/'开头时默认是从此类所在的包下取资源，以’/'开头则是从ClassPath根下(即'/'代表src)获取。
//        InputStream input = PropertiesUtility.class.getClassLoader()
//                .getResourceAsStream("\\config.properties");

    }

    /**
     * 1.test文件夹已设置为Resource Folder
     */
    private static void demo1() {

        InputStream resourceAsStream1 = GetResourceAsStream.class.getClassLoader().getResourceAsStream("te.properties");
        String message1 = resourceAsStream1 == null ? "ClassLoader(不+/)获取不到" : "ClassLoader(不+/)可以获取";
        System.out.println("\t" + message1);

        InputStream resourceAsStream2 = GetResourceAsStream.class.getClassLoader().getResourceAsStream("/te.properties");
        String message2 = resourceAsStream2 == null ? "ClassLoader(+/)获取不到" : "ClassLoader(+/)可以获取";
        System.out.println("\t" + message2);

        InputStream resourceAsStream3 = GetResourceAsStream.class.getResourceAsStream("te.properties");
        String message3 = resourceAsStream3 == null ? "class(不+/)获取不到" : "class(不+/)可以获取";
        System.out.println("\t" + message3);

        InputStream resourceAsStream4 = GetResourceAsStream.class.getResourceAsStream("/te.properties");
        String message4 = resourceAsStream4 == null ? "class(+/)获取不到" : "class(+/)可以获取";
        System.out.println("\t" + message4);

    }

    /**
     * 2.资源文件在src目录下的config文件夹中
     */
    private static void demo2() {
        InputStream resourceAsStream1 = GetResourceAsStream.class.getClassLoader().getResourceAsStream("config/mysql2.properties");
        String message1 = resourceAsStream1 == null ? "ClassLoader(不+/)获取不到" : "ClassLoader(不+/)可以获取";
        System.out.println("\t" + message1);

        InputStream resourceAsStream2 = GetResourceAsStream.class.getClassLoader().getResourceAsStream("/config/mysql2.properties");
        String message2 = resourceAsStream2 == null ? "ClassLoader(+/)获取不到" : "ClassLoader(+/)可以获取";
        System.out.println("\t" + message2);

        InputStream resourceAsStream3 = GetResourceAsStream.class.getResourceAsStream("config/mysql2.properties");
        String message3 = resourceAsStream3 == null ? "class(不+/)获取不到" : "class(不+/)可以获取";
        System.out.println("\t" + message3);

        InputStream resourceAsStream4 = GetResourceAsStream.class.getResourceAsStream("/config/mysql2.properties");
        String message4 = resourceAsStream4 == null ? "class(+/)获取不到" : "class(+/)可以获取";
        System.out.println("\t" + message4);
    }

    /**
     * 3.资源文件和类在同一目录
     */
    private static void demo3() {

        InputStream resourceAsStream1 = GetResourceAsStream.class.getClassLoader().getResourceAsStream("mysql.properties");
        String message1 = resourceAsStream1 == null ? "ClassLoader(不+/)获取不到" : "ClassLoader(不+/)可以获取";
        System.out.println("\t" + message1);

        InputStream resourceAsStream2 = GetResourceAsStream.class.getClassLoader().getResourceAsStream("/mysql.properties");
        String message2 = resourceAsStream2 == null ? "ClassLoader(+/)获取不到" : "ClassLoader(+/)可以获取";
        System.out.println("\t" + message2);

        InputStream resourceAsStream3 = GetResourceAsStream.class.getResourceAsStream("mysql.properties");
        String message3 = resourceAsStream3 == null ? "class(不+/)获取不到" : "class(不+/)可以获取";
        System.out.println("\t" + message3);

        InputStream resourceAsStream4 = GetResourceAsStream.class.getResourceAsStream("/mysql.properties");
        String message4 = resourceAsStream4 == null ? "class(+/)获取不到" : "class(+/)可以获取";
        System.out.println("\t" + message4);
    }

    /**
     * 资源在src根目录下
     */
    private static void demo4() {

        InputStream resourceAsStream1 = GetResourceAsStream.class.getClassLoader().getResourceAsStream("mysql3.properties");
        String message1 = resourceAsStream1 == null ? "ClassLoader(不+/)获取不到" : "ClassLoader(不+/)可以获取";
        System.out.println("\t" + message1);

        InputStream resourceAsStream2 = GetResourceAsStream.class.getClassLoader().getResourceAsStream("/mysql3.properties");
        String message2 = resourceAsStream2 == null ? "ClassLoader(+/)获取不到" : "ClassLoader(+/)可以获取";
        System.out.println("\t" + message2);

        InputStream resourceAsStream3 = GetResourceAsStream.class.getResourceAsStream("mysql3.properties");
        String message3 = resourceAsStream3 == null ? "class(不+/)获取不到" : "class(不+/)可以获取";
        System.out.println("\t" + message3);

        InputStream resourceAsStream4 = GetResourceAsStream.class.getResourceAsStream("/mysql3.properties");
        String message4 = resourceAsStream4 == null ? "class(+/)获取不到" : "class(+/)可以获取";
        System.out.println("\t" + message4);
    }
}